﻿using PizzaService.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace PizzaService.Converters
{
    class ZutatenConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                string zutatenList = String.Empty;

                Type ValueType = value.GetType(); // get the type of the provided value
                if (ValueType.Name == typeof(List<>).Name || ValueType.Name == typeof(ObservableCollection<>).Name) // Check if we're dealing with a list
                {
                    foreach (var item in (IList)value)//if so, loop thru all items in the list
                    {
                        if (item.GetType() == typeof(Zutat))
                        {
                            var tempZutat = (Zutat)item;

                            zutatenList += tempZutat.Name + ", ";
                        }
                        else if (item.GetType() == typeof(ExtraZutat))
                        {
                            var tempZutat = (ExtraZutat)item;

                            zutatenList += tempZutat.Name + ", ";
                        }
                    }

                    zutatenList = zutatenList.TrimEnd(", ".ToCharArray());
                }
                return zutatenList;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
